<template>
  <div class="login">
    <div class="content">
      <h1 class="title">登录账号</h1>
      <div class="user-account">
        <el-form
          :model="loginForm"
          status-icon
          :rules="rules"
          ref="loginForm"
          class="demo-loginForm"
          hide-required-asterisk
        >
          <el-form-item  prop="account">
            <el-input
              type="text"
              v-model="loginForm.account"
              autocomplete="off"
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item  prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              autocomplete="off"
              show-password
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitBtn">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { checkLogin } from "../../api/user";
export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 8, message: "长度在 4 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitBtn() {
      //验证表单
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // alert('submit')
          let res = await checkLogin(this.loginForm);
          console.log(res);
          //验证通过
          let { code, msg, role, token } = res;
          if (code === 0) {
            localStorage.setItem("tk", token);
            this.$message({
              message: msg,
              type: "success",
            });
            this.$router.push("/");
          } else {
            this.$message.error(msg);
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  // background-color: #2d3a4b;
  background: linear-gradient(-125deg, #ff7d7d, #7386fb);
  position: relative;
  .content {
    background-color: #fff;
    width: 350px;
    height: 380px;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    border-radius: 10px;
    box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.4);
    .title {
      color: rgb(2, 2, 2);
      text-align: center;
      margin-top: 50px;
    }
    .user-account {
      margin-top: 50px;
      .el-form-item {
        padding: 0 20px;
        margin-top: 35px;
      }
    }
    button {
      width: 100%;
      height: 40px;
      /*       margin-left: 30px;
      margin-top: 30px; */
      background-color: #409eff;
      color: #fff;
      border-radius: 5px;
      cursor: pointer;
    }
    .el-input {
      width: 100%;
    }
  }
}
</style>